package com.jlwl.houseservice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jlwl.houseservice.common.Result;
import com.jlwl.houseservice.entity.ServiceRequest;
import com.jlwl.houseservice.service.ServiceRequestService;
import com.jlwl.houseservice.vo.ServiceRequestQueryVO;
import com.jlwl.houseservice.vo.ServiceRequestReplyVO;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 管理员服务需求控制器
 */
@RestController
@RequestMapping("/admin/service-requests")
@PreAuthorize("hasRole('ADMIN')")
public class AdminServiceRequestController {

    @Autowired
    private ServiceRequestService serviceRequestService;

    /**
     * 获取服务需求列表
     */
    @GetMapping
    public Result<Page<ServiceRequest>> list(ServiceRequestQueryVO queryVO) {
        Page<ServiceRequest> page = serviceRequestService.pageAdminList(queryVO);
        return Result.success(page);
    }

    /**
     * 获取服务需求详情
     */
    @GetMapping("/{id}")
    public Result<ServiceRequest> getDetail(@PathVariable Integer id) {
        ServiceRequest serviceRequest = serviceRequestService.getAdminServiceRequestDetail(id);
        if (serviceRequest == null) {
            return Result.error("服务需求不存在");
        }
        return Result.success(serviceRequest);
    }

    /**
     * 回复服务需求
     */
    @PutMapping("/{id}/reply")
    public Result<Void> reply(@PathVariable Integer id, @Valid @RequestBody ServiceRequestReplyVO replyVO) {
        boolean success = serviceRequestService.replyServiceRequest(id, replyVO);
        if (!success) {
            return Result.error("回复服务需求失败");
        }
        return Result.success();
    }

    /**
     * 更新服务需求状态
     */
    @PutMapping("/{id}/status")
    public Result<Void> updateStatus(@PathVariable Integer id, @RequestParam String status) {
        boolean success = serviceRequestService.updateServiceRequestStatus(id, status);
        if (!success) {
            return Result.error("更新服务需求状态失败");
        }
        return Result.success();
    }
} 